package cn.jingzhuan.element.demo.view.fragment

import android.content.Context
import android.os.Bundle
import android.support.annotation.MenuRes
import android.support.v7.widget.GridLayoutManager
import android.view.MenuItem
import android.view.View
import cn.jingzhuan.element.demo.view.BaseActivity
import cn.jingzhuan.element.demo.view.BaseBindingFragment
import cn.jingzhuan.element.demo.view.adapter.BaseRecyclerAdapter
import cn.jingzhuan.element.demo.view.adapter.RecyclerViewHolder
import cn.jingzhuan.element.demo.view.decoration.GridDividerItemDecoration
import cn.jingzhuan.element.demo.view.model.ItemDescription
import cn.jingzhuan.element.element.R
import cn.jingzhuan.element.element.databinding.FragmentHomeBinding
import cn.jingzhuan.element.element.databinding.ItemDescriptionBinding

abstract class HomeFragment :BaseBindingFragment<FragmentHomeBinding>(){

    private var itemAdapter: ItemAdapter? = null
    override fun getLayoutId(): Int = R.layout.fragment_home
//    private var homeControlListener: HomeControlListener? = null

    override fun onDataBind(savedInstanceState: Bundle?, binding: FragmentHomeBinding) {
        // 初始化工具栏
        initToolbar()

        // 初始化数据源
        initRecyclerView()
    }

    private fun initRecyclerView() {
        itemAdapter = getItemAdapter()
        itemAdapter?.setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener{
            override fun onItemClick(itemView: View, pos: Int) {
                val item = itemAdapter?.getItem(pos)
                try {
                    onHomeControlListener().startActivity(item?.demoClass!!,item.name!!)
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
        })
        binding.recyclerView.adapter = itemAdapter
        val spanCount = 3
        binding.recyclerView.layoutManager =  GridLayoutManager(context, spanCount)
        binding.recyclerView.addItemDecoration(GridDividerItemDecoration(context!!, spanCount))
    }

    abstract fun onHomeControlListener():HomeControlListener

    interface HomeControlListener {
        fun startActivity(activityCla: Class<out BaseActivity>?,title:String)
    }

    /**
     * Toolbar 添加菜单功能
     */
    fun addAction(@MenuRes menuAction:Int?,listenerMap: Map<Int,MenuItem.OnMenuItemClickListener>?) {
        if (null != menuAction) {
            binding.toolBar.inflateMenu(menuAction!!)

            if(listenerMap!= null && listenerMap.isNotEmpty()) {
                listenerMap.forEach({
                    binding.toolBar.menu.findItem(it.key).setOnMenuItemClickListener(it.value)
                })
            }
        }
    }


    /**
     *
     */
    private fun initToolbar() {
        binding.toolBar.title = getTitle()
    }


    protected abstract fun getTitle(): String

    protected abstract fun getItemAdapter(): ItemAdapter

    class ItemAdapter(ctx: Context, data: MutableList<ItemDescription>) : BaseRecyclerAdapter<ItemDescription>(ctx, data) {

        override fun getItemLayoutId(viewType: Int): Int {
            return R.layout.home_item_layout
        }

        override fun bindData(holder: RecyclerViewHolder, position: Int, item: ItemDescription) {
            holder.getTextView(R.id.item_name).text = item.name
            holder.getImageView(R.id.item_icon).setImageResource(item.iconRes)
        }

    }

}